﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Net.Mail;
using System.Configuration;
using System.IO;
using System.Net;
using System.Text;

/// <summary>
/// Summary description for Email
/// </summary>
public class Contact
{
    public class ContactForm
    {

        public static readonly string ApplicationName = ConfigurationManager.AppSettings["ApplicationName"];
        public static readonly string EmailDefaultSender = ConfigurationManager.AppSettings["MailDefaultSender"];
        public static readonly string EmailServerHostname = ConfigurationManager.AppSettings["EmailServerHostname"];
        public static readonly string EmailServerUsername = ConfigurationManager.AppSettings["MailServerUsername"];
        public static readonly string EmailServerPassword = ConfigurationManager.AppSettings["MailServerPassword"];
        public static readonly int EmailServerPortnumber = Convert.ToInt32(ConfigurationManager.AppSettings["MailServerPort"]);

        /// <summary>
        /// Sends an e-mail. Returns TRUE or FALSE indicating whether the e-mail was properly sent.
        /// </summary>
        public static bool Mail(MailAddress Recipient, String Subject, bool IsVisiter)
        {
            Boolean isSent = false;

            if (Recipient != null)
            {
                SmtpClient SMTP = new SmtpClient();
                SMTP.Host = EmailServerHostname;
                SMTP.Port = EmailServerPortnumber;
                SMTP.UseDefaultCredentials = false;
                SMTP.Credentials = new NetworkCredential(EmailServerUsername, EmailServerPassword);

                MailMessage mailMsg = new MailMessage();
                mailMsg.BodyEncoding = System.Text.Encoding.Default;

                mailMsg.Subject = Subject;
                mailMsg.IsBodyHtml = true;

                if (IsVisiter)
                {
                    mailMsg.From = new MailAddress(EmailDefaultSender);
                    mailMsg.To.Add(Recipient);
                }
                else
                {
                    mailMsg.From = Recipient;
                    mailMsg.To.Add(new MailAddress(EmailDefaultSender));
                }

                if (Convert.ToBoolean(ConfigurationManager.AppSettings["DebugMode"]) == true)
                {
                    mailMsg.To.Clear();
                    mailMsg.To.Add(new MailAddress("YOUREMAIL"));
                }

                mailMsg.Body = "<html>";
                mailMsg.Body += "<head>";
                mailMsg.Body += "<style type=\"text/css\">";
                mailMsg.Body += "body, td, th{font:8pt Verdana;}";
                mailMsg.Body += "div {float:left;width:150px;;}";
                mailMsg.Body += "</style>";
                mailMsg.Body += "</head>";
                mailMsg.Body += "<body>";

                if (IsVisiter)
                {
                    mailMsg.Body += "<table cellspacing=\"0\" cellpadding=\"5\"> ";
                    mailMsg.Body += "<tr> <td> Your message has been received in good order. </ td></ tr>";
                    mailMsg.Body += "<tr> <td> Below is an overview of the information you provided. </ td></ tr>";
                    mailMsg.Body += "</table>";
                }

                mailMsg.Body += "<table cellspacing=\"0\" cellpadding=\"5\"> ";

                mailMsg.Body += "<tr><td width=\"125px;\"><b>Information:</b></td><td></td><td></td></tr>";
                mailMsg.Body += "<tr><td>Homepage</td><td>:</td><td>" + "PLACEHOLDER NOT IMPLEMENTED YET" + "</td></tr>";
                mailMsg.Body += "</table>";


                if (IsVisiter)
                {
                    mailMsg.Body += "<p><b>****** DISCLAIMER ******</b><br />";
                    mailMsg.Body += "<p>The information contained in this e-mail is confidential and may be privileged. It may be read, copied and used only by the intended recipient. If you have received it in error, please contact the sender immediately by return email; please delete in this case the email and do not disclose its contents to any person. We don\"t accept liability for any errors, omissions, delays of receipt or viruses in the contents of this message which arise as a result of e-mail transmission.</p>";
                }

                mailMsg.Body += "</body>";
                mailMsg.Body += "</html>";

                try
                {
                    SMTP.Send(mailMsg);
                    if (IsVisiter)
                    {
                        Mail(Recipient, "Registration on CompanyName", false);
                    }
                    isSent = true;
                }
                catch
                { isSent = false; }

                mailMsg.Dispose();
            }
            return isSent;
        }
    }
}